<template>
  <div class="card-container">
    <div class="card">
      <div class="card-header">
        <div class="card-title">{{ cardData.title }}</div>
        <div class="card-value">{{ cardData.value }}</div>
      </div>
      <div class="card-content">
        <BaseCharts
            ref="baseChartsRef1"
            :options="chartOptions"
            :autoRander="autoRander"
            :autoNumber="autoNumber"
            :height="height"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import {computed, ref} from "vue";
import BaseCharts from "@/components/echarts/vpt-base-chart/index.vue";

const props = defineProps({
  cardData: {type: Object, default: () => ({})},
  pieData: {type: Array, default: () => []},
  legendPosition: {type: String, default: "right"},
  autoRander: {type: Boolean, default: false},
  autoNumber: {type: Number, default: 60},
  height: {type: String, default: "2rem"},
});

const chartOptions = computed(() => {
  return {
    tooltip: {
      trigger: "item",
      formatter: "{a} <br/>{b} : {c} ({d}%)",
    },
    legend: {
      orient: "vertical",
      [props.legendPosition]: "20%",
      top: "center",
      data: props.pieData.map((item) => item.name),
      formatter: function (name) {
        const dataItem = props.pieData.find((item) => item.name === name);
        return `${name}        ${dataItem.value} (${dataItem.percentage})`;
      },
    },
    series: [
      {
        name: "资产分布",
        type: "pie",
        radius: "80%",
        center: ["21%", "50%"],
        data: props.pieData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)",
          },
        },
        label: {
          show: false,
        },
        labelLine: {
          show: false,
        },
      },
    ],
  };
});
</script>
<style lang="less">
.card-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  padding: 1rem 0.1rem;
  margin-top: 0.6rem;

  .card {
    width: 100%;
    background: #f5f5f5;
    border-radius: 0.1rem;
    text-align: center;

    .card-header {
      padding: 0.1rem;
      background: #fff;
      border-radius: 0.1rem 0.1rem 0 0;
      margin-right: 4rem;
      margin-top: 0.2rem;
      display: flex;
      flex-direction: column;
      align-items: flex-start;

      .card-title {
        font-size: 0.16rem;
        padding: 0.05rem;
        color: #707987;
      }

      .card-value {
        font-size: 0.18rem;
        color: #1e222a;
        padding: 0.05rem;
      }
    }

    .card-content {
      padding: 0.11rem;
      background: #fff;
      border-radius: 0 0 0.1rem 0.1rem;
    }
  }
}
</style>